//index.js
//获取应用实例
const app = getApp()
var util = require('../../utils/util.js');
var URL = app.globalData.URL;
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    src: 'http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400',
    releaseFocus: false,
    releaseName: '',  //输入框的placeholder
    duration: 10,
    bottom: '0',
    commentList: {},     //评论列表
    currenttime: '',      //播放进度时间
    freeTime: '',         //视频免费试看时间
    video: {             //video中各种参数
      showVideo: 'false'
    },
    showVideo: false,
    modelHide: true,    //显示模态框
    modelHide2: true,
    indexPageHide: false, //首页显示情况
    pos: 'relative',     //视频定位样式 
    videoHeight: '',      //视频高度
    sectionHeight: '',
    recommendList: {},    //推荐专题列表
    recommendHidden: false,  //推荐专题是否显示
    topicList: {},         //专题视频列表
    likedImg: 'imgs/like_03.png',        //喜欢图片地址
    buyCarImg: 'imgs/buy_car.png',         //购物车图片地址
    nowVideoTitle: '',     //当前视频名称
    formatCtime: '',      //当前视频日期
    data_id: '',         //当前视频的id(课时id)
    nowPrice: '',        //当前视频的价格
    album_id: '42146',        //课程id
    albumDetailId: '',          //课程集详情id
    agree_flag: '',       //判断当前课程集用户是否已点赞,0否1是
    agree_nr: '',         //该课程喜欢人数
    album_pay_flag: '',    //判断该课程是否已购买
    nowCoverImg: '',      //当前视频封面图
    commentLikeImg: 'imgs/thumb_up_03.png',   //评论是否点赞的图片
    thumbImgHidden: 'true',    //点赞图片显示问题
    videoAbstract: '',     //当前课程集的描述
    commentContent: '',    //输入框的内容
    commentFristId: '',   //第一层评论id
    commentAutoId: '',    //评论作者的id
    dataThumbUpImg: '',   //评论点赞图片src
    ifLikeLess: true,    //是否喜欢该课程，true不喜欢，false喜欢（图片显示）
    firstLikeImg: false,     //第一次渲染点赞图标显示
    likeImgArr: [],     //存放点赞标志数组
    show_play_btn: true,  //是否显示播放按钮
    sendHidden: true,   //评论框是否显示，true隐藏 false显示
    show_buyAlbumBtn: false,
    btnFlag: 0,       //按钮样式 
    accountFlag: 0,   //账号登录输入框样式
    buyAlbumModal: true,   //选择支付方式弹窗
    accountLogin: true,   //账号登录弹窗
    title: '',         //课程集名称
    price: '',        //课程集优惠价格
    origin_price: '',   //课程集原价格
    userNameInput: '',   //账号登录用户名
    passwordInput: '',   //账号登录密码
    lesson_id: '',      //保存课时或课程集id，用于支付
    lesson_type: '',     //保存课程类型，502为课程集，503课时
    lesson_name: '',     //保存课程或课时名称
    lesson_price: '',    //保存课程价格
    videoEnd: true,        //当视频试看结束时，显示‘试看结束’
    showBuyAlbum: false, //课程集购买按钮是否显示
  },

  /**
   * 点击封面播放视频
  */
  playVideo: function () {
    this.setData({
      'showVideo': true,

    })
    //触发视频播放
    this.videoContext = wx.createVideoContext('myVideo')
    this.videoContext.play()
  },
  /**
   * 视频播放时触发
  */
  bindplay: function () {
    this.setData({
      bottom: '60px',    //播放触发时改变文字位置
    })
  },

  /**
  * 点击回复
  */
  bindReply: function (e) {
    this.setData({
      releaseFocus: true
    })
  },

  //指定视频时长
  Duration: function (e) {
    this.setData({
      action: {
        method: ''
      }
    })
  },

  /**
   * 视频播放中监控
  */
  timeupdata: function (res) {
    var _this = this;
    var currentTime = res.detail.currentTime;  //获取播放进度
    var album_pay_flag = _this.data.album_pay_flag; //true为已购买
    var album_type = 'keshi';
    var freeTime = _this.data.freeTime;
    if (album_pay_flag == false) {
      if (currentTime >= freeTime) {
        _this.bindpauseVideo();
        _this.setData({
          //modelHide: false,
          modelHide2: true,
          //show_play_btn: false,
          videoEnd: false,
        })
        var lesson_type = 'keshi';
        this.judgeBind(lesson_type);
      }
    }
    this.setData({
      currenttime: currentTime
    })
  },

  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  bindpauseVideo: function () {
    this.videoContext.pause()
  },
  onReady: function (res) {
    this.videoContext = wx.createVideoContext('myVideo')
  },

  bindButtonTap: function () {
    var that = this
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: ['front', 'back'],
      success: function (res) {
        that.setData({
          src: res.tempFilePath
        })
      }
    })
  },

  /**
   * 获取当前专题id
  */
  getEnterAlumbId: function () {
    var _this = this;
    var album_id = _this.data.album_id;
    _this.getWeikeAlbumDetail(album_id)
    _this.getTopicsVideoList(album_id);return;
    wx.request({
      url: URL + '/index.php?app=interface&mod=WeikeAlbum&act=weixinAppGetAlbumid' + app.globalData.URLBOOL,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var album_id = res.data.data.album_id;
        if (res.data.error_code == 0) {
          _this.getWeikeAlbumDetail(album_id)
          _this.getTopicsVideoList(album_id);
        } else {
          wx.showToast({
            title: res.data.error_msg,
          })
        }

      }
    })
  },

  /**
   * 点击专题视频改变播放地址
   */
  changeVideoUrl: function (res) {
    var _this = this;
    var videoSrc = res.currentTarget.dataset.src + "&token=" + wx.getStorageSync('token');
    var nowVideoTitle = res.currentTarget.dataset.title;
    var data_id = res.currentTarget.dataset.id;
    var formatCtime = res.currentTarget.dataset.ctime;
    var nowPrice = res.currentTarget.dataset.nowprice;
    var nowCoverImg = res.currentTarget.dataset.cover_url;
    var lesson_pay_flag = res.currentTarget.dataset.album_pay_flag;
    var agree_flag = res.currentTarget.dataset.agree_flag;
    var agree_nr = res.currentTarget.dataset.agree_nr;
    var descript = res.currentTarget.dataset.descript;
    var hint_time = res.currentTarget.dataset.hint_time;
    var index = res.currentTarget.dataset.index;  //当前视频索引值
    if (agree_flag == 0) {
      this.setData({
        ifLikeLess: true,

      })
    } else {
      this.setData({
        ifLikeLess: false,
      })
    }
    this.getComment(data_id);
    if (nowPrice == 0) {
      _this.setData({
        nowPrice: ''
      })
    } else if (lesson_pay_flag == true) {
      _this.setData({
        nowPrice: "已购买"
      })
    } else {
      this.setData({
        nowPrice: "￥" + nowPrice
      })
    }
    this.setData({
      src: videoSrc,
      data_id: data_id,
      nowVideoTitle: nowVideoTitle,
      formatCtime: formatCtime,
      nowCoverImg: nowCoverImg,
      album_pay_flag: lesson_pay_flag,
      agree_flag: agree_flag,
      agree_nr: agree_nr,
      videoAbstract: descript,
      freeTime: hint_time,
      showVideo: false,
      show_play_btn: true,
    })
  },

  //获取评论列表
  getComment: function (typeId) {
    var _this = this;
    wx.request({
      url: URL + '/interface/Comment/getCommentsWithFloor?newer=1&type=8&curId=0&num=0&typeId=' + typeId + app.globalData.URLBOOL,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {

        var is_support = res.data;
        if (res.data.error_code == 0) {
          _this.setData({
            commentList: res.data
          })
        } else if (res.data.error_code == 10002) {
          _this.setData({
            likeImgArr: '',
            commentList: ''
          })
        }
        _this.likecondition();
      }
    })
  },

  /**
   * 点击评论弹出键盘
  */
  showSend: function () {
    var _this = this;
    _this.setData({
      sendHidden: false,
      releaseFocus: true,
      releaseName: "请输入您的留言",
      show_buyAlbumBtn: true,
    })
  },

  /**
   * 点击他人评论回复
  */
  showSend2: function (res) {
    var user_name = res.currentTarget.dataset.name;
    var commentFristId = res.currentTarget.dataset.id
    this.setData({
      sendHidden: false,
      releaseFocus: true,
      releaseName: "回复" + user_name + "...",
      show_buyAlbumBtn: true,
      commentFristId: commentFristId
    })
  },

  /**
   * 点击页面其他地方隐藏评论框
  */
  hiddenSendInput: function () {
    var _this = this;
    _this.setData({
      sendHidden: true,
      show_buyAlbumBtn: false
      //releaseFocus: true
    })
  },

  /**
     * 获取课程集课时列表
    */
  getTopicsVideoList: function (album_id) {
    var _this = this;
    var album_id = _this.data.album_id;
    wx.request({
      url: URL + '/index.php?app=album&mod=Ajax&act=getWeikeAlbumLessonList&album_id=' + album_id + app.globalData.URLBOOL,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data.error_code == 0) {
          if (res.data.data != '') {
            var recommend_video_url = res.data.data[0].resource_list[0].resource_url; //获取专题视频地址
            var nowVideoTitle = res.data.data[0].resource_list[0].title
            var nowCoverImg = res.data.data[0].resource_list[0].face; //获取当前视频封面图
            var data_id = res.data.data[0].id;
            var formatCtime = res.data.data[0].format_ctime;
            var nowPrice = res.data.data[0].price;
            if (nowPrice == 0) {
              nowPrice = '';
            } else {
              nowPrice = "￥" + res.data.data[0].price
            }

            var name = res.data.data[0].name;     //当前视频名称
            var album_pay_flag = res.data.data[0].lesson_pay_flag;  //当前课时是否付费
            var agree_flag = res.data.data[0].agree_flag;
            var agree_nr = res.data.data[0].agree_nr;
            var videoAbstract = res.data.data[0].description;         //获取当前课程集描述
            var freeTime = res.data.data[0].hint_at;         //获取当前课程时长
            //var nowVideoPrice = res.data.data
            _this.getComment(data_id);
            /*wx.setNavigationBarTitle({                          //修改标题
              title: nowVideoTitle//页面标题为路由参数
            })*/
            if (album_pay_flag == true && nowPrice != 0) {
              nowPrice = "已购买"
            }
            //判断该课程是否已点赞，并改变图片显示
            if (agree_flag == 0) {
              _this.setData({
                ifLikeLess: true
              })
            } else {
              _this.setData({
                ifLikeLess: false
              })
            }
            _this.setData({
              topicList: res.data.data,
              src: recommend_video_url + "&token=" + wx.getStorageSync('token'),
              nowVideoTitle: nowVideoTitle,
              data_id: data_id,
              //nowPrice: nowPrice,
              formatCtime: formatCtime,
              nowCoverImg: nowCoverImg,
              //nowVideoTitle: name,
              album_pay_flag: album_pay_flag,
              agree_flag: agree_flag,
              agree_nr: agree_nr,
              videoAbstract: videoAbstract,
              freeTime: freeTime

            })
            if (nowPrice == 0) {
              _this.setData({
                nowPrice: ''
              })
            } else if (album_pay_flag == true) {
              _this.setData({
                nowPrice: "已购买"
              })
            } else {
              _this.setData({
                nowPrice: nowPrice
              })
            }

          } else {
            _this.setData({
              src: '',
              //nowVideoTitle: nowVideoTitle,
              data_id: '',
              nowPrice: '',
              formatCtime: '',
              nowCoverImg: '',
              nowVideoTitle: '',
              album_pay_flag: '',
              topicList: res.data.data,
            })
            _this.getComment();
          }



        }
        setTimeout(function () {
          wx.hideLoading();
        }, 1000)
      }
    })

  },

  /**
     * 课程集详情(推荐专题列表)
    */
  getRecommendList: function () {
    var _this = this;
    wx.request({
      url: URL + '/index.php?app=webapp&mod=Ajax&act=getRecommandAlbum' + app.globalData.URLBOOL,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {

        if (res.data.error_code == 0) {
          _this.setData({
            recommendList: res.data.data
          })
        } else if (res.data.error_code == 10002) {
          _this.setData({
            recommendHidden: true
          })
        }

      }
    })
  },

  /**
   * 课程集详情
  */
  getWeikeAlbumDetail: function (id) {
    var _this = this;
    wx.request({
      url: URL + '/index.php?app=webapp&mod=Ajax&act=getWeikeAlbumDetail&id=' + id + app.globalData.URLBOOL,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res);
        var album_pay_flag = res.data.data.album_pay_flag;  //判断课程是否支付
        var agree_flag = res.data.data.agree_flag;          //判断该用户是否已点赞，0否1是
        var agree_nr = res.data.data.agree_nr;              //判断该课程集多少人喜欢
        var name = res.data.data.name;                      //获取当前课程集名称
        var videoAbstract = res.data.data.abstract;         //获取当前课程集描述
        var album_id = res.data.data.id;                    //获取当前课程集id
        var cheap_price = res.data.data.price;              //课程集优惠价格
        var origin_price = res.data.data.origin_price;      //课程集原始价格
        wx.setNavigationBarTitle({                          //修改标题
          title: name//页面标题为路由参数
        })
        if (agree_flag == 0) {
          _this.setData({
            likedImg: 'imgs/like_03.png'
          })
        } else {
          _this.setData({
            likedImg: 'imgs/liked_03.png'
          })
        }
        //判断课程集是否已支付
        if (album_pay_flag == false) {
          _this.setData({
            showBuyAlbum: false,  //显示购买按钮，隐藏已购买
          })
        } else {
          _this.setData({
            showBuyAlbum: true,  //隐藏购买按钮，显示已购买
          })
        }
        _this.setData({
          //agree_flag: agree_flag,
          //agree_nr: agree_nr,
          //album_pay_flag: album_pay_flag,
          //videoAbstract: videoAbstract,
          album_id: album_id,
          title: name,
          price: cheap_price,
          origin_price: origin_price,
          //nowVideoTitle: name
        })
      }
    })

  },

  /**
   * 获取账号
  */
  userNameInput: function (res) {
    var userName = res.detail.value;
    this.setData({
      userNameInput: userName
    })
  },

  /**
   * 获取密码
  */
  passwordInput: function (res) {
    var passwordInput = res.detail.value;
    this.setData({
      passwordInput: passwordInput
    })
  },

  /**
   * 微信账号与系统账号进行绑定
  */
  Accountbind: function (data, callback) {
    var _this = this;
    wx.request({
      url: URL + '/index.php?app=interface&mod=WeixinApp&act=bindAcount&token=' + wx.getStorageSync('token'),
      method: 'POST',
      data: data,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {   //开发者服务返回的回调函数

        if (typeof callback == 'function') {
          callback(res)
        }

      }
    })
  },

  /**
   * 系统账号登录
  */
  account_to_buy: function () {
    var _this = this;
    var data = {};
    var userNameInput = _this.data.userNameInput;
    var passwordInput = _this.data.passwordInput;
    var price = _this.data.lesson_price.substring(1);
    var name = _this.data.lesson_name;
    data.username = userNameInput;
    data.password = passwordInput;
    data.type = _this.data.lesson_type;
    data.type_id = _this.data.lesson_id;
    _this.Accountbind(data, function (res) {
      _this.index();
      //console.log(res.data.data.pay_flag)
      if (res.data.error_code == 0) {
        try {
          //wx.clearStorageSync();
          wx.setStorageSync('bindAcountBool', true);
          wx.setStorageSync('token', res.data.data.token); //更改token

        } catch (e) {

        }
        if (res.data.data.pay_flag == false) {
          app.judgeToken(_this, function () {
            app.creatOrder(data.type, data.type_id, price, name, _this);
            _this.setData({
              modelHide: false,
              //show_play_btn: false,

            })
          })
          _this.index();
        } else {
          wx.showToast({
            title: '您已购买',
          })
          _this.setData({
            accountLogin: true,
            buyAlbumModal: true,
          })
          _this.index();
        }
      } else {
        wx.showToast({
          title: res.data.error_msg,
        })
      }

    })
  },

  /**
   * 点击推荐专题
  */
  toDetailList: function (res) {
    var _this = this;
    var album_id = res.currentTarget.dataset.id;   //该课程集的id
    _this.setData({
      album_id: album_id
    })
    _this.getWeikeAlbumDetail(album_id);
    _this.getTopicsVideoList(album_id);
  },

  /**
   * 支付中心列表
  */
  paymentList: function () {
    var _this = this;
    wx.request({
      url: URL + '/index.php?jsonVar=add&app=interface&mod=Action&act=addSupport&type=1&id=2993&token=' + wx.getStorageSync('token'),
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: data, //需要传递给后台的数据
      success: function (res) {   //开发者服务返回的回调函数

      }
    })
  },

  /**
   * 添加评论
  */
  sendComment: function () {
    var _this = this;
    var content = _this.data.commentContent
    var data = {};

    data.content = content;
    var placeHolder = _this.data.releaseName;
    var data_id = _this.data.data_id;
    if (placeHolder == '请输入您的留言') {
      data.typeId = data_id;
      data.type = 8;
    } else {
      data.pid = _this.data.commentFristId;
      data.puid = _this.data.commentAutoId;
    }
    app.judgeToken(_this, function () {
      wx.request({
        url: URL + '/index.php?app=interface&mod=Comment&act=addComment&token=' + wx.getStorageSync('token'),
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: data, //需要传递给后台的数据
        success: function (res) {   //开发者服务返回的回调函数

          if (res.data.error_code == 0) {
            var commentFristId = res.data.data.id
            _this.setData({
              commentFristId: commentFristId,
              commentContent: '',
              sendHidden: true,
              buyAlbumBtn_pos: 'fixed',
              show_buyAlbumBtn: false,
            })
            _this.getComment(data_id)
          }
        },

      })
    })

  },

  contentInput: function (res) {
    var content = res.detail.value
    this.setData({
      commentContent: content
    })
  },



  /**
   * 点赞喜欢课程操作
  */
  thumbUp: function (res) {
    var agree_falg = res.currentTarget.dataset.flag
    var _this = this;
    //var id = res.currentTarget.dataset.id;   //获取当前元素的id值
    var id = _this.data.data_id;
    var likedUrl = _this.data.likedImg;
    var agree_nr_new = parseInt(_this.data.agree_nr) + 1;
    var topicList = _this.data.topicList;
    if (agree_falg == true) {
      _this.likedImg(id, function () {
        for (var i = 0; i < topicList.length; i++) {
          if (topicList[i].id == id) {
            topicList[i].agree_flag = true;
          }
        }
        _this.setData({
          ifLikeLess: false,
          agree_nr: agree_nr_new,
          topicList: topicList
        })
        //_this.getWeikeAlbumDetail(id)
      })
    } else {
      _this.likedCancel(id, function () {
        for (var i = 0; i < topicList.length; i++) {
          if (topicList[i].id == id) {
            topicList[i].agree_flag = false;
          }
        }
        var agree_nr_new = parseInt(_this.data.agree_nr) - 1;
        _this.setData({
          ifLikeLess: true,
          agree_nr: agree_nr_new,
          topicList: topicList
        })
        //_this.getWeikeAlbumDetail(id)
      })
    }
    /*if (likedUrl == 'imgs/like_03.png') {
      _this.likedImg(id);
    } else {
      _this.likedCancel(id);
    }*/
  },

  //喜欢操作
  likedImg: function (id, callback) {
    var _this = this;
    app.judgeToken(_this, function () {
      wx.request({
        url: URL + '/index.php?app=interface&mod=Action&act=addSupport&type=503&id=' + id + '&token=' + wx.getStorageSync('token'),
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {   //开发者服务返回的回调函数

          if (res.data.error_code == 0) {
            /*_this.setData({
              likedImg: 'imgs/liked_03.png'
            })*/
            if (typeof callback == 'function') {
              callback();
            }
          } else {
            wx.showToast({
              title: res.data.error_msg,
            })
          }
        },
      })
    })

  },

  //取消喜欢
  likedCancel: function (id, callback) {
    var _this = this;
    app.judgeToken(_this, function () {
      wx.request({
        url: URL + '/index.php?app=interface&mod=Action&act=deleteSupport&type=503&id=' + id + '&token=' + wx.getStorageSync('token'),
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {   //开发者服务返回的回调函数

          /*_this.setData({
            likedImg: 'imgs/like_03.png'
          })*/
          if (res.data.error_code == 0) {
            if (typeof callback == 'function') {
              callback();
            }
          } else {
            wx.showToast({
              title: res.data.error_msg,
            })
          }
        },
      })
    })

  },

  /**
   * 点击评论赞操作
  */
  likecondition: function () {
    var _this = this;
    var list = _this.data.commentList.data
    var likeImgArr = [];
    if (list) {
      for (var i = 0; i < list.length; i++) {
        likeImgArr[i] = list[i]
      }
      _this.setData({
        likeImgArr: likeImgArr
      })
    }

  },

  getImg: function (res) {
    var _this = this
    var id = res.currentTarget.dataset.id;            // 获取id
    var data_id = _this.data.data_id
    //var is_support = res.currentTarget.dataset.index;  //0未点赞
    var album_id = _this.data.album_id;               //获取课程集id
    var index = res.currentTarget.dataset.eq;       //获取当前索引值
    //var list = _this.data.likeImgArr;
    var list = _this.data.commentList.data;
    var new_list = [];
    var support_flag = list[index].agree_flag;
    if (support_flag == 0) {
      list[index].agree_flag = 1;
      _this.likeComment(id, function () {
        list[index].agree_flag = 1;
        list[index].agree_nr++;
        _this.setData({
          likeImgArr: list
        })
      });
      _this.setData({
        likeImgArr: list
      })
    } else {
      list[index].agree_flag = 0;
      _this.likeOperation(id, function () {
        list[index].agree_flag = 0;
        list[index].agree_nr--;
        _this.setData({
          likeImgArr: list
        })
      })
    }
  },
  /**
   * 点赞评论
  */
  likeComment: function (id, callback) {
    var _this = this;
    app.judgeToken(_this, function () {
      wx.request({
        url: URL + '/index.php?app=interface&mod=Action&act=addSupport&type=1&id=' + id + '&token=' + wx.getStorageSync('token'),
        header: {

        },
        success: function (res) {

          if (res.data.error_code == 0) {
            if (typeof callback == 'function') {
              callback();
            }
          } else {
            wx.showToast({
              title: res.data.error_msg,
            })
          }

        }
      })
    })

  },

  /**
   * 取消点赞评论操作
  */
  likeOperation: function (id, callback) {
    var _this = this;
    app.judgeToken(_this, function () {
      wx.request({
        url: URL + '/index.php?app=interface&mod=Action&act=deleteSupport&type=1&id=' + id + '&token=' + wx.getStorageSync('token'),
        header: {

        },
        success: function (res) {

          if (res.data.error_code == 0) {
            if (typeof callback == 'function') {
              callback();
            }
          } else {
            wx.showToast({
              title: res.data.error_msg,
            })
          }
        }
      })
    })

  },

  /**
   * 点击中心 跳转至支付中心
  */
  to_payment_center: function () {
    wx.navigateTo({
      url: '../payment_center/payment_center',
      success: function (res) {
      }
    })
  },

  /**
   * 购买课程
  */
  buyLesson: function (lesson_id) {
    var _this = this;
    app.judgeToken(_this, function () {
      _this.setData({
        modelHide2: false,
        show_play_btn: true
      })
    })
    //app.creatOrder(503, id, price, name, _this);  
  },

  /**
   * 关闭购买弹窗
  */
  close_payment: function () {
    this.setData({
      modelHide: true,
      modelHide2: true,
      buyAlbumModal: true,
      accountLogin: true
    })
  },

  /**
   * 点击全部显示列表页
  */
  showListPage: function () {
    var width = wx.getSystemInfoSync().windowWidth
    var videoH = width * 9 / 16;
    this.setData({
      indexPageHide: true,
      pos: 'fixed',
      sectionHeight: videoH + 50 + 'px',
      bottom: '30px'
    })
  },
  showListPage2: function () {
    var width = wx.getSystemInfoSync().windowWidth
    var videoH = width * 9 / 16;
    this.setData({
      indexPageHide: false,
      pos: 'relative',
      sectionHeight: videoH + 20 + 'px',
      bottom: '0px'
    })
  },
  /**
   * 视频播放结束
  */
  videoEnded: function () {
    var album_pay_flag = this.data.album_pay_flag;
    var album_type = 'keshi';
    if (album_pay_flag == false) {
      if (currentTime >= 60) {
        this.bindpauseVideo();
        this.setData({
          //modelHide: false,
          modelHide2: true,
          //show_play_btn: false,
          videoEnd: false,
        })
        var lesson_type = 'keshi';
        this.judgeBind(lesson_type);
      }
    }
  },
  /**
   * 购买课程
  */
  buyLesson2: function () {
    var _this = this;
    var id = this.data.lesson_id;
    var price = this.data.lesson_price.substring(1);
    var name = this.data.lesson_name;
    var pay_type = this.data.lesson_type
    app.judgeToken(_this, function () {
      app.creatOrder(pay_type, id, price, name, _this);
      _this.setData({
        modelHide: false,
        //show_play_btn: false,

      })

    })
  },

  /**
   * 点击使用雨校账号购买
  */
  account_login: function () {
    var _this = this;
    _this.setData({
      btnFlag: 0
    })
  },

  /**
   * 点击直接支付
  */
  direct_pay: function () {
    var _this = this;
    _this.setData({
      btnFlag: 1
    })
  },

  /**
   * 打包购买的点击事件
  */
  buyAlbum: function () {
    var lesson_type = 'album';
    this.setData({
      videoEnd: true,
    })
    this.judgeBind(lesson_type)
  },

  /**
   * 购买单个课时的点击事件
  */
  buyKeshi: function () {
    var lesson_type = 'keshi';
    this.setData({
      videoEnd: true
    })
    this.judgeBind(lesson_type);
  },

  /**
   * 判断用户账号是否已绑定
  */
  judgeBind: function (lesson_type) {
    var _this = this;
    var bind_acount_bool = wx.getStorageSync('bindAcountBool');
    //var lesson_type = res.currentTarget.dataset.type
    if (lesson_type == 'album') {
      var lesson_id = _this.data.album_id;
      var lesson_name = _this.data.title;
      var lesson_price = _this.data.price;
      _this.setData({
        lesson_id: lesson_id,
        lesson_type: 502,
        lesson_name: lesson_name,
        lesson_price: '￥' + lesson_price
      })
    } else {
      var lesson_id = _this.data.data_id;
      var lesson_name = _this.data.nowVideoTitle;
      var lesson_price = _this.data.nowPrice;
      _this.setData({
        lesson_id: lesson_id,
        lesson_type: 503,
        lesson_name: lesson_name,
        lesson_price: lesson_price,
      })
    }
    //如果未绑定，则可选择账号登录，若绑定了则直接支付
    if (bind_acount_bool == false) {
      _this.buyAllAlbum(lesson_id);
    } else {
      _this.buyLesson(lesson_id);
    }
  },

  /**
   * 选择支付方式后点击确定按钮，若选择账号0 则跳转至账号登录弹窗
  */
  after_choose_pay: function () {
    var _this = this;
    var lesson_id = _this.data.lesson_id;   //当前专题id
    var name = _this.data.lesson_name;          //当前专题名称
    var price = _this.data.lesson_price.substring(1);         //当前专题价格
    var lesson_type = _this.data.lesson_type; //当前视频类型，课时或课程集 
    var flag = _this.data.btnFlag;       //flag为0为选择系统账号登录
    if (flag == 0) {
      _this.setData({
        accountLogin: false,
        buyAlbumModal: true
      })
    } else {
      _this.setData({
        modelHide2: true,
        modelHide: true,
        buyAlbumModal: true
      })
      app.judgeToken(_this, function () {
        app.creatOrder(lesson_type, lesson_id, price, name, _this);

      })
    }
  },

  /**
   * 账号密码输入框获取焦点时的样式改变
  */
  typeAccount: function () {
    var _this = this;
    _this.setData({
      accountFlag: 0
    })
  },

  /**
   * 
  */
  typePassword: function () {
    var _this = this;
    _this.setData({
      accountFlag: 1
    })
  },

  /**
   * 点击购买课程集
  */
  buyAllAlbum: function (lesson_id) {
    var _this = this;
    _this.setData({
      buyAlbumModal: false
    })
  },

  /**
   * 重新选择支付方式
  */
  chooseAgain: function () {
    var _this = this;
    _this.setData({
      accountLogin: true,
      buyAlbumModal: false
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: 'VIP助考',
      path: 'pages/index/index',
      imageUrl: "./imgs/forward.jpg",
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  onLoad: function (options) {
    wx.showLoading({
      title: 'loading...',
    });
    var channel_no = wx.getStorageSync('channel_no');
    var option = options;
    if ((JSON.stringify(option) == '{}' || !option) && !channel_no) {
      wx.setStorageSync('channel_no', '');
    } else if (JSON.stringify(option) == '{}' || !option) {
      wx.setStorageSync('channel_no', channel_no);
    } else {
      wx.setStorageSync('channel_no', options.channel_no);
    }
    var width = wx.getSystemInfoSync().windowWidth    //获取屏幕宽度
    var videoH = width * 9 / 16;                      //设置video高度
    this.setData({
      album_id: option.id,
      videoHeight: videoH + 'px',
      sectionHeight: videoH + 20 + 'px'
    })
    var _this = this;
    _this.index();
    // app.login(_this);
  },
  index: function () {
    app.setUrlBool();
    this.getEnterAlumbId();
    this.getRecommendList();
  },
  userInfoHandler: function (info) {
    var _this = this;
    if (info.detail.errMsg == "getUserInfo:ok" ) {
      if (!wx.getStorageSync('setAuth') || wx.getStorageSync('setAuth') == false) {
        wx.setStorageSync('setAuth', true);
        app.setUserInfoAndUpdateInfo(info.detail.userInfo);
      }
    }else {
      wx.setStorageSync('setAuth', false);
      _this.setData({
        sendHidden: true,
        show_buyAlbumBtn: false,
      })
    }
  }
})
